/**
 * Created by huguowei on 2017/4/24.
 */
import Axios from 'axios'

const url = 'http://www.lingworkshop.cn/vd/api/'
// const url = 'http://172.30.118.164:8081/api/'

const axios = Axios.create({
  baseURL: url,
  timeout: 10000,
  withCredentials: true,
  headers: {
    'Content-Type': 'application/json',
    Accept: 'application/json',
    token: sessionStorage.getItem('token')
  }
})

export default {
  name: 'service',
  video: {
    list () {
      return axios.get('video/list/0')
    },
    create () {
    },
    add () {
      return axios.post()
    },
    delete () {
    }
  },
  car: {
    list () {
      return axios.get('cart/list')
    },
    add () {
      return axios.post('cart/add')
    },
    addCourse (params) {
      return axios.post('cart/add-course', params)
    },
    addChapter (params) {
      return axios.post('cart/add', params)
    }
  },
  pay: {
    wechart (tradeNo) {
      return axios.get(`pay/wxpay/${tradeNo}`)
    },
    alipay (tradeNo) {
      return axios.get(`pay/alipay/${tradeNo}`)
    }
  },
  user: {
    list () {
      return axios.get('wxuser/list')
    },
    get () {
      return axios.get('userinfo/get')
    }
  },
  chapter: {
    create (params, config) {
      return axios.post('chapter/add', params, config)
    },
    update (params, config) {
      return axios.post('chapter/update', params, config)
    },
    list (id, noChild, config) {
      const theId = id || 0
      let u = `chapter/list-with-child/${theId}`
      if (noChild) {
        u = `chapter/list/${theId}`
      }
      return axios.get(u, config)
    },
    get (id, noChild, config) {
      const theId = id || ''
      let u = `chapter/get-with-child/${theId}`
      if (noChild) {
        u = `chapter/get/${theId}`
      }
      return axios.get(u, config)
    },
    delete (id, config) {
      return axios.post('chapter/delete', {
        id
      }, config)
    },
    getPurchaseStatus (cid, sid, config = {}) {
      return axios.get(`chapter/isbuy/${cid}/${sid}`, config)
    },
    getNext () {
    },
    getPrev () {
    }
  },
  course: {
    create (params, config) {
      return axios.post('course/add', params, config)
    },
    update (params, config) {
      return axios.post('course/update', params, config)
    },
    list (id, noChild, config) {
      const theId = id || ''
      let u = `course/list-with-child/${theId}`
      if (noChild) {
        u = `course/list/${theId}`
      }
      return axios.get(u, config)
    },
    get (id, noChild, config) {
      const theId = id || ''
      let u = `course/get-with-child/${theId}`
      if (noChild) {
        u = `course/get/${theId}`
      }
      return axios.get(u, config)
    },
    delete (id, config) {
      return axios.post('course/delete', {
        id
      }, config)
    }
  },
  order: {
    list (config) {
      return axios.get('order/list', config)
    },
    gen (ids) {
      return axios.post('order/generate', {
        buyIds: ids
      })
    },
    get (tradeNo) {
      return axios.get(`order/get/${tradeNo}`)
    },
    delete (tradeNo) {
      return axios.get(`order/delete/${tradeNo}`)
    }
  }
}
